package org.weasel.preferences;

import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.weasel.controllers.PreferenceController;

public class GeneralPage extends PreferencePage implements
		IWorkbenchPreferencePage {

	private Text txtDataFileLocation;

	private Button checkRoundTabs;

	private PreferenceController preferenceController;

	private Button checkMaximized;

	private Button fileChooserButton;

	private Button checkBurndown;

	@Override
	protected Control createContents(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		container.setLayout(layout);
		layout.numColumns = 3;
		layout.verticalSpacing = 9;

		Label label = new Label(container, SWT.NULL);
		label.setText("Datafile location:");

		txtDataFileLocation = new Text(container, SWT.BORDER | SWT.SINGLE);
		txtDataFileLocation.setEditable(false);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.grabExcessHorizontalSpace = true;
		gd.horizontalSpan = 1;
		txtDataFileLocation.setLayoutData(gd);
		
		fileChooserButton = new Button(container,SWT.NONE);
		fileChooserButton.setImage(new Image(Display.getCurrent(), getClass().getResourceAsStream("/icons/folder.gif")));
		fileChooserButton.addMouseListener(new org.eclipse.swt.events.MouseAdapter() {
			public void mouseUp(org.eclipse.swt.events.MouseEvent e) {
				PreferenceController.instance().selectXMLFilePath();
			}
		});
		gd = new GridData(GridData.BEGINNING);
		gd.horizontalSpan = 1;
		fileChooserButton.setLayoutData(gd);
		

		Label line = new Label(container, SWT.SEPARATOR | SWT.HORIZONTAL
				| SWT.BOLD);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 3;
		gd.verticalIndent = 9;
		line.setLayoutData(gd);

		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.grabExcessHorizontalSpace = true;
		gd.horizontalSpan = 3;

		checkBurndown = new Button(container, SWT.CHECK);
		checkBurndown.setText("display burn-down detail per day (otherwise per iteration)");
		checkBurndown.setLayoutData(gd);
		
		checkRoundTabs = new Button(container, SWT.CHECK);
		checkRoundTabs.setText("use traditional style tabs");
		checkRoundTabs.setLayoutData(gd);

		checkMaximized = new Button(container, SWT.CHECK);
		checkMaximized.setText("maximize Window at startup");
		checkMaximized.setLayoutData(gd);

		line = new Label(container, SWT.SEPARATOR | SWT.HORIZONTAL | SWT.BOLD);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 3;
		gd.verticalIndent = 9;
		line.setLayoutData(gd);

//		gd = new GridData(GridData.FILL_HORIZONTAL);
//		gd.grabExcessHorizontalSpace = true;
//		gd.horizontalSpan = 3;
//		Label label1 = new Label(container, SWT.NULL);
//		label1.setText("Attention, a restart is needed to let the changes take effect!");
//		label1.setLayoutData(gd);

		// load set preferences into the fields
		preferenceController.loadPageValues();

		return container;
	}

	public void init(IWorkbench workbench) {
		// set the preferenceStore
		this.preferenceController = PreferenceController.instance();
		this.preferenceController.setGeneralPage(this);
		setPreferenceStore(preferenceController.getPrefStore());
	}

	/**
	 * performs ok...for those changes that have to be tracked down because of
	 * updates in the model a corresponding method in the preferenceController
	 * is called. the others are simply set in the preferenceStore
	 */
	public boolean performOk() {
		preferenceController.savePreferences();
		return super.performOk();
	}

	/**
	 * resets all the fields values to their defaults
	 */
	public void performDefaults() {
		preferenceController.restoreDefaults();
	}

	// getters of Page controls

	public Button getCheckRoundTabs() {
		return checkRoundTabs;
	}

	public Text getTxtDataFileLocation() {
		return txtDataFileLocation;
	}

	public Button getCheckBurndown() {
		return checkBurndown;
	}

	public Button getCheckMaximized() {
		return checkMaximized;
	}
}
